package com.wanggsx.library.util.fun;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;

public class UtilsGlide {

    private static DiskCacheStrategy mStrategy = DiskCacheStrategy.NONE;
    private static ScaleType scaleType = ScaleType.centerInside;

    public static void setStrategy(DiskCacheStrategy mStrategy) {
        UtilsGlide.mStrategy = mStrategy;
    }

    public static void setScaleType(ScaleType scaleType) {
        UtilsGlide.scaleType = scaleType;
    }

    public enum ScaleType{
        centerInside,centerCrop,fitCenter,circleCrop
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, Drawable drawable, ImageView imageView){
        context = context.getApplicationContext();
        RequestBuilder options = Glide.with(context).load(drawable).diskCacheStrategy(mStrategy).dontAnimate();
        if (scaleType == ScaleType.centerInside) {
            options.centerInside();
        } else if (scaleType == ScaleType.centerCrop) {
            options.centerCrop();
        } else if (scaleType == ScaleType.fitCenter) {
            options.fitCenter();
        } else if (scaleType == ScaleType.circleCrop) {
            options.circleCrop();
        }
        options.into(imageView);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView){
        loadImage(context, url, imageView, scaleType, null, 0, mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView, RequestListener listener){
        loadImage(context, url, imageView, scaleType, listener, 0, mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView, int detaultResourceId){
        loadImage(context, url, imageView, scaleType, null, detaultResourceId,mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView,ScaleType scaleType){
        loadImage(context, url, imageView, scaleType, null, 0,mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView, ScaleType scaleType,int detaultResourceId){
        loadImage(context, url, imageView, scaleType, null, detaultResourceId,mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView, RequestListener listener,int detaultResourceId){
        loadImage(context, url, imageView, scaleType, listener, detaultResourceId,mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView,ScaleType scaleType, RequestListener listener,int detaultResourceId){
        loadImage(context, url, imageView, scaleType, listener, detaultResourceId,mStrategy);
    }

    /** 使用Glide加载图片 */
    public static void loadImage(Context context, String url, ImageView imageView, ScaleType scaleType,RequestListener listener,int detaultResourceId, DiskCacheStrategy strategy) {
        context = context.getApplicationContext();
        RequestBuilder options = Glide.with(context).load(url).diskCacheStrategy(strategy).dontAnimate();
        if (scaleType == ScaleType.centerInside) {
            options.centerInside();
        } else if (scaleType == ScaleType.centerCrop) {
            options.centerCrop();
        } else if (scaleType == ScaleType.fitCenter) {
            options.fitCenter();
        } else if (scaleType == ScaleType.circleCrop) {
            options.circleCrop();
        }
        if (detaultResourceId > 0) {
            options.placeholder(detaultResourceId);
        }
        options.addListener(listener).into(imageView);
    }

    /** 清理内存缓存 */
    public static void clearMemoryCache(Context context){
        Glide.get(context).clearMemory();
    }

    /** 清理图片缓存 */
    public static void clearFileCache(final Context context){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();
            }
        }).start();
    }

}
